/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.JButton;
import org.aswing.JScrollPane;

import com.bourre.commands.Delegate;

import fever.data.file.File;
import fever.data.libs.BitmapLib;
import fever.data.libs.BitmapLibEvent;
import fever.log.FeverDebug;

import flash.display.BitmapData;

import fvaswing.components.filechooser.mvc.FvFileChooserView;
import fvaswing.components.filechooser.preview.FvChooserPreview;
import fvaswing.components.filechooser.preview.FvImagePopup;
import fvaswing.components.filechooser.preview.FvPreviewCommand;
import fvaswing.components.FvBitmapIcon;
import fvaswing.components.FvLabelButton;
import fvaswing.visual.FvViewHelper;

/**
 * Concrete implementation of image preview loading.
 * 
 * <p>Use <strong>Pixlib data lib</strong>API.
 * 
 * <p>Use <strong>Fever BitmapLib</strong> specific library to store loaded 
 * image in buffer.
 * 
 * @see fvaswing.components.filechooser.preview.FvPreviewCommand
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.filechooser.preview.FvImagePreview extends FvPreviewCommand
{	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _rComponent : Component;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	public static var BASE_PATH : String = "";
		
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function FvImagePreview()
	{
		super();
	}	
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _execute( item : File ) : Void
	{
		if( item.getContent() )
		{
			FeverDebug.DEBUG( "get buffer" );
			_buildAndSendComponent( item );
		}
		else
		{
			FeverDebug.DEBUG( "load image" );
			var lib : BitmapLib = new BitmapLib();
			lib.addEventListener( BitmapLib.onLoadProgressEVENT, this, _onImageProgress );
			lib.addEventListener( BitmapLib.onLoadInitEVENT, Delegate.create( this, _onImageLoaded, item ) );			lib.addEventListener( BitmapLib.onTimeOutEVENT, this, _onImageError );			lib.addEventListener( BitmapLib.onErrorEVENT, this, _onImageError );
			lib.setName( item.getPath() );
			lib.load( BASE_PATH + item.getPath() );
		}
	}
	
	private function _onImageProgress( event : BitmapLibEvent ) : Void
	{
		var percent : Number = event.getLib().getPerCent();
		FvChooserPreview( FvViewHelper.getViewHelper( FvFileChooserView.PREVIEW ) ).handleProgress( percent );
	}
	
	private function _onImageLoaded( event : BitmapLibEvent, item : File ) : Void
	{
		item.setContent( event.getBitmap() );
		_buildAndSendComponent( item );
	}
	
	private function _onImageError( event : BitmapLibEvent ) : Void
	{
		_fireError();
	}
	
	private function _buildAndSendComponent( item : File ) : Void
	{
		var bmp : BitmapData = BitmapData( item.getContent() );
		var label : FvLabelButton = new FvLabelButton( '', new FvBitmapIcon( bmp, bmp.width, bmp.height ) );
		var size : String = bmp.width + "x" + bmp.height;
		
		label.setToolTipText( size + '\ndbl-click to open in new window' );
		label.addEventListener( JButton.ON_CLICKED, Delegate.create( this, _handleOpenPopup, item ) );
		
		_rComponent = new JScrollPane( 
			label, 
			JScrollPane.SCROLLBAR_AS_NEEDED, 
			JScrollPane.SCROLLBAR_AS_NEEDED
		);
		
		_fireResult( _rComponent );	
	}
	
	private function _handleOpenPopup( source : FvLabelButton, clickCount : Number, item : File ) : Void
	{
		if( clickCount == 2 )
		{
			FvImagePopup.open( item );
		}
	}
}